package com.dataart.training.crimequalification.dao;

import com.dataart.training.crimequalification.entity.CrimeSubjectEntity;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CrimeSubjectDAOImpl implements CrimeSubjectDAO {

    //-----------------------------------------------------------------

    @Autowired
    private SessionFactory sessionFactory;

    // ----------------------------------------------------------------

    /**
     *
     * @return
     */
    @Override
    public List<CrimeSubjectEntity> getAllCrimeSubjects() {
        return sessionFactory.getCurrentSession()
                .createQuery("from CrimeSubjectEntity order by codeName").list();
    }

    /**
     *
     * @param codename
     * @return
     */
    @Override
    public CrimeSubjectEntity getCrimeSubjectByCodename(String codename) {
        return (CrimeSubjectEntity) sessionFactory.getCurrentSession().
                createCriteria(CrimeSubjectEntity.class)
                .add(Restrictions.eq("codeName", codename)).uniqueResult();
    }
}
